package org.ssh2.core.entity;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;







/**
 * 用户实体
 * 林景锋
 * 2017年11月23日
 * lv1.0
 */
@Entity
@Table(name = "soft_user")
public class SoftUser extends BaseEntity implements UserDetails {

	/**
	 * unique=true是指这个字段的值在这张表里不能重复，所有记录值都要唯一，就像主键那样
	 * nullable=false是这个字段在保存时必需有值，不能还是null值就调用save去保存入库
	 *
	 *private Integer id;
	private String userId;
	private String userName;
	private String userRealName;
	private Integer userSex;
	private Integer userAge;
	private String userPassword;
	private Integer userType;
	private String userAddress;
	private String userTelephone;
	private String userEmail;
	private String userPerId;
	private Integer userPerType;
	private String labId;
	private String sign;
	 */
	private static final long serialVersionUID = -7996992202295686955L;


	@Column(name = "user_create_date")
	private String createDate;// 创建日期

	@Column(name = "user_name", unique = true, nullable = false)
	private String userName;// 用户名

	@Column(name = "user_password")
	private String userPassword;//密码

	@Column(name = "user_telephone")
	private String userTelephone; // 电话

	@Column(name = "user_address")
	private String userAddress;//地址
	
	@Column(name = "user_email")
	private String userEmail;//邮箱
	
	@Column(name = "user_realname")
	private String userRealName;//真实姓名
	
	@Column(name = "user_sex")
	private Integer userSex;//性别:1-男；2-女
	
	@Column(name = "user_age")
	private Integer userAge;//年龄
	
	@Column(name = "user_idcard")
	private String userIDcard;//身份证
	
	//inverse表示控制反转，inner join什么的，都是hibernate映射java实体对象类的一些映射约束了什么的
	@ManyToMany(fetch = FetchType.EAGER)//多对多
	@JoinTable(name = "soft_user_role", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "role_id") )
	private Set<SoftRole> user_role;// 所属角色

	public SoftUser() {

	}

	public String getCreateDate() {
		return createDate;
	}





	public void setCreateDate(String createDate) {
		this.createDate = createDate;
	}





	public String getUserName() {
		return userName;
	}





	public void setUserName(String userName) {
		this.userName = userName;
	}





	public String getUserPassword() {
		return userPassword;
	}





	public void setUserPassword(String userPassword) {
		this.userPassword = userPassword;
	}





	public Set<SoftRole> getUser_role() {
		return user_role;
	}





	public void setUser_role(Set<SoftRole> user_role) {
		this.user_role = user_role;
	}





	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		Set<GrantedAuthority> auths = new HashSet<>();
		Set<SoftRole> roles = this.getUser_role();
		// 默认所有的用户有"USER"的权利
		auths.add(new SimpleGrantedAuthority("ROLE_USER"));
		System.out.println("auths="+auths);
		for (SoftRole role : roles) {

			auths.add(new SimpleGrantedAuthority(role.getRoleName()));
			System.out.println("roles==="+auths);
		}
		System.out.println("auths==="+auths);
		return auths;
	}

	@Override
	public String getPassword() {
		return this.userPassword;
	}

	@Override
	public String getUsername() {
		return this.userName;
	}

	@Override
	public boolean isAccountNonExpired() {
		return true;
	}

	@Override
	public boolean isAccountNonLocked() {
		return true;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		return true;
	}

	@Override
	public boolean isEnabled() {
		return true;
	}
}
